// Copyright (c) 2009 http://grommet.codeplex.com
// This source is subject to the Microsoft Public License.
// See http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.

using System;
using Microsoft.SPOT;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;

namespace Grommet.Net.Test
{
    public class Program
    {
        public static void Main()
        {
            string soapRequest = @"<?xml version=""1.0"" encoding=""utf-8""?>" +
                                @"<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">" +
                                @"<soap:Body>" +
                                @"<HelloWorld xmlns=""http://grommet.codeplex.com/GrommetService"" />" +
                                @"</soap:Body>" +
                                @"</soap:Envelope>";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/GrommetService/Greeting.asmx");
            request.Headers.Add("SOAPAction", "http://grommet.codeplex.com/GrommetService/HelloWorld");
            request.ContentType = "text/xml; charset=utf-8";
            request.Method = "POST";
            Stream stream = request.GetRequestStream();
            byte[] buffer = Encoding.UTF8.GetBytes(soapRequest); 
            stream.Write(buffer, 0, buffer.Length);

            WebResponse response = request.GetResponse();
            Debug.Print("Web Service response:");
            using (XmlTextReader reader = new XmlTextReader(response.GetResponseStream()))
            {
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name == "HelloWorldResult")
                        {
                            reader.Read();
                            Debug.Print(reader.Value);
                        }
                    }
                }
            }
        }
    }
}
